<template>
    <!--不带参数logout() 和logout 一样-->
  <el-container class="home_container">
    <!--header区-->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>

      <el-button type="info" @click="logout()">退出</el-button>
    </el-header>
    <!--主体区-->
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#429CFE"
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="currentPath"
          router
        >
          <!--一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!--一级菜单的模板-->
            <template slot="title">
              <!--图标-->
              <i :class="iconObject[item.id]"></i>
              <!--文本-->
              <span v-text="item.authName"></span>
            </template>

            <!--二级菜单-->
            <!--使用组件时可以使用官方推荐的select事件，也可是click这类常用事件-->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveCurrentPath('/'+subItem.path)">
              <!--二级菜单的模板-->
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span v-text="subItem.authName"></span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
    export default {
        data() {
            return {
                menuList: [],
                iconObject: {
                    '125': 'el-icon-user',
                    '103': 'el-icon-goods',
                    '102': 'el-icon-s-order',
                    '101': 'el-icon-s-home',
                    '145': 'el-icon-s-operation',
                },
                isCollapse: false,
                currentPath: '',
            }
        },
        created() {
            this.getMenuList();
            this.currentPath=window.sessionStorage.getItem('currentPath')
        },
        methods: {
            /*selectPath(index, indexPath) {
                this.currentPath = index
                window.sessionStorage.setItem('currentPath', this.currentPath)
            },*/
            saveCurrentPath(index) {
                this.currentPath = index
                window.sessionStorage.setItem('currentPath', this.currentPath)
            },
            logout() {
                window.sessionStorage.clear()
                this.$router.push('/login')
            },
            async getMenuList() {
                let {data: res} = await this.$http.get('menus');
                if (res.meta.status !== 200) {
                    return this.$message.error(res.meta.msg)
                }
                this.menuList = res.data
            },
            toggleCollapse() {
                this.isCollapse = !this.isCollapse
            }
        }
    }
</script>

<style scoped>
  .el-aside {
    background-color: #313743;
  }
  .el-menu {
    border-right: none;
  }
  .el-header {
    background-color: #363D3F;
    /*padding 如果不生效就往父容器里面找*/
    padding-left: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #eee;
  }
  .el-header > div {
    display: flex;
    /*justify-content: space-between;*/
    align-items: center;
  }
  .el-header > div > span {
    margin-left: 15px;
  }
  img {
    width: 50px;
    height: 50px;
    border-radius: 50%
  }
  .el-main {
    background-color: #E9EDF0;
  }
  .home_container {
    height: 100%;
  }
  .toggle-button {
    background-color: #475162;
    text-align: center;
    line-height: 25px;
    color: #eee;
    font-size: 10px;
    letter-spacing: 0.2em;
    cursor: pointer;

  }
</style>
